<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

use Acme\DemoBundle\Entity\Comments;
use Acme\DemoBundle\Form\ContactType;
use Acme\DemoBundle\Form\CategoryType;
use Acme\DemoBundle\Form\CommentsType;
  

use Acme\DemoBundle\Entity\Category;
use Acme\DemoBundle\Entity\Newss;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DemoController extends Controller
{
    /**
     * @Route("/", name="_demo")
     * @Template()
     */
    public function indexAction()
    {
        
                $form=$this->createForm(new CommentsType());
                return $this->render('AcmeDemoBundle:Demo:comments.html.twig', array(
                                        'form' => $form->createView()));
    }
    /**
     * @Route("/addcomment", name="_demo_addcomment")
     * @Template()
     */
    public function addcommentAction(Request $request)
    {
        $comment=new Comments();

         $form = $this->createForm(new CommentsType(),$comment);
         

             if ($request->getMethod() == 'POST') {
                    $form->bindRequest($request);
                    
                 if ($form->isValid()) {
                     
                     //$comment = $form->getData();
                    
                        $em = $this->getDoctrine()->getEntityManager();
                        $em->persist($comment);
                        $em->flush();
                     
                     
                     return new Response("SSSSSSSSSSSS");
                     
                 }
                 
                 return new Response("P".$comment->getName());
             }
            return array();
    }

    /**
     * @Route("/hello/{name}", name="_demo_hello")
     * @Template()
     */
    public function helloAction($name)
    {
        return array('name' => $name);
    }

    /**
     * @Route("/contact", name="_demo_contact")
     * @Template()
     */
    public function contactAction()
    {
        $form = $this->get('form.factory')->create(new ContactType());

        $request = $this->get('request');
        if ('POST' == $request->getMethod()) {
            $form->bindRequest($request);
            if ($form->isValid()) {
                $mailer = $this->get('mailer');
                // .. setup a message and send it
                // http://symfony.com/doc/current/cookbook/email.html

                $this->get('session')->setFlash('notice', 'Message sent!');

                return new RedirectResponse($this->generateUrl('_demo'));
            }
        }

        return array('form' => $form->createView());
    }
    /**
     * @Route("/createcat", name="_demo_createcat")
     * @Template()
     */
    public function createcatAction(Request $request)
    {
        $category = new Category();
         $form = $this->createFormBuilder($category)->add('name', 'text')->getForm();

             if ($request->getMethod() == 'POST') {
                    $form->bindRequest($request);

                 if ($form->isValid()) {
                    
                     $em = $this->getDoctrine()->getEntityManager();
                        $em->persist($category);
                        $em->flush();
                     
                     
                     return new Response("SSSSSSSSSSSS");
                     
                 }
                 
                 return new Response("P");
             }
             return array();
          
    }
    /**
     * @Route("/news", name="_demo_news")
     * @Template()
     */
    public function newsAction()
    {
	
	
		$category = new Category();
                $category->setName("abd");
                $form=$this->createForm(new CategoryType(),$category);
                return $this->render('AcmeDemoBundle:Demo:news.html.twig', array(
                                        'form' => $form->createView(),));

		
	/*	

	
	
	    $c = $this->getDoctrine()
        ->getRepository('AcmeDemoBundle:Category')
        ->findAll();
		$cats=array();
		foreach($c as $cat)
		{
			$cats[]=array('id'=>$cat->getId(),'name'=>$cat->getName());
		}
        return array('cats'=>$cats);
		*/
    }
	/**
     * @Route("/show/{id}", name="_demo_show")
     * @Template()
     */
    public function showAction($id)
    {
          $c = $this->getDoctrine()
        ->getRepository('AcmeDemoBundle:Category')
        ->find($id);

		if (!$c) {
			throw $this->createNotFoundException('No Category found for id '.$id);
		}
		$n = $this->getDoctrine()
        ->getRepository('AcmeDemoBundle:Newss')
        ->findByCat($id);
		$newss=array();
		foreach($n as $news)
		{
			$newss[]=array('id'=>$news->getId(),'name'=>$news->getTitle());
		}
		return array('cats'=>$newss);
		
		
    }
	/**
     * @Route("/d/{id}", name="_demo_d")
     * @Template()
     */
	public function dAction($id)
    {
          $c = $this->getDoctrine()
        ->getRepository('AcmeDemoBundle:Newss')
        ->find($id);

		if (!$c) {
			throw $this->createNotFoundException('No News found for id '.$id);
		}
		$news = $this->getDoctrine()
        ->getRepository('AcmeDemoBundle:Newss')
        ->find($id);
	
		
		return array('news'=>array('id'=>$news->getId(),'title'=>$news->getTitle(),'body'=>$news->getBody()));
		
		
    }
	
}
